package cn.com.template.controller.system.admin;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.commons.Paginator;
import cn.com.commons.utils.StringUtil;
import cn.com.dal.dataobject.hr.DeptDO;
import cn.com.dal.dataobject.hr.RoleDO;
import cn.com.dal.dataobject.hr.UserDO;
import cn.com.dal.dataobject.ref.RefUserRoleDO;
import cn.com.service.hr.DeptService;
import cn.com.service.hr.UserService;
import cn.com.service.ref.RefUserRoleService;
import cn.com.service.sys.RoleService;
import cn.com.support.context.UserContext;
import cn.com.support.message.MessageResult;
import cn.com.support.utils.JacksonUtil;
import cn.com.template.ui.Flexigrid;
import cn.com.template.utils.FlexigridUtil;

/**
 * 
 * 用户控制器
 * 
 * @since 1.0 2012-2-21 下午11:03:53
 * @author Zoe
 */
@Controller
@RequestMapping("/system/admin/usermanager")
public class UserManagerController {

	@Autowired
	private UserService userService;
	@Autowired
	private DeptService deptService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private RefUserRoleService userRoleService;
	
	@RequestMapping
	public ModelAndView forwardUserManager(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("system/admin/usermanager");
		List<DeptDO> deptLst = deptService.selectList(null);
		request.setAttribute("deptLst", deptLst);
		List<RoleDO> roleDOLst = roleService.selectList(null);
		request.setAttribute("roleLst", roleDOLst);
		return mav;
	}
	
	@RequestMapping("/getUserInfo")
	@ResponseBody
	public Flexigrid getUserInfo(HttpServletRequest request, HttpServletResponse response){
		Flexigrid flexigrid = null;
		try {
			Paginator<UserDO, List<UserDO>> paginator = new Paginator<UserDO, List<UserDO>>();
			//deptname
			String nickName = request.getParameter("nickName");
			String deptName = request.getParameter("deptName");
			UserDO userDO = new UserDO();
			if(StringUtil.isNotBlank(nickName)){
				userDO.setNickname(nickName);
			}
			if(StringUtil.isNotBlank(deptName)){
				userDO.setDeptName(deptName);
			}
			paginator.setCondition(userDO);
			// 设置分页器
			paginator.setPageSize(Integer.parseInt(request.getParameter("rp")));
			paginator.setCurrentPage(Integer.parseInt(request.getParameter("page")));
			userService.selectByPage(paginator);
			//PACKAGE FLEXIGRID DATA
			flexigrid = FlexigridUtil.formatFlexigridData(paginator);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flexigrid;
	}

	@RequestMapping(value="/addUserInfo", method=RequestMethod.POST)
	@ResponseBody
	public MessageResult<Long> addUserInfo(HttpServletRequest request){
		MessageResult<Long> result = null;
		UserDO userDO = null;
		RefUserRoleDO userRoleDO = null;
		try {
			String jsonData = request.getParameter("data");
			//防止js harmful数据入库
			jsonData = jsonData.replaceAll("\"", "\\\"");
			userDO = (UserDO) JacksonUtil.json2Bean(jsonData, UserDO.class);
			userDO.setStatus((byte)1);
			userDO.setGmtLogin(new Date());
			userDO.setCreator(UserContext.getUsername());
			result = userService.insert(userDO);
			if(result.isSucceed()){
				//权限数据入库
				String[] roleIds = userDO.getRoleIds();
				Long userId = userDO.getId();
				for(String roleId : roleIds){
					userRoleDO = new RefUserRoleDO();
					userRoleDO.setUserId(userId);
					userRoleDO.setRoleId(Long.parseLong(roleId.trim()));
					userRoleDO.setCreator(UserContext.getUsername());
					result = userRoleService.insert(userRoleDO);
				}
			}else{
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@RequestMapping("/updateUserInfo")
	@ResponseBody
	public MessageResult<Integer> updateUserInfo(HttpServletRequest request){
		MessageResult<Integer> result = null;
		UserDO userDO = null;
		try {
			String jsonData = request.getParameter("data");
			userDO = (UserDO) JacksonUtil.json2Bean(jsonData, UserDO.class);
			String password = userDO.getPassword();
			if(!StringUtil.isNotBlank(password)){
				userDO.setPassword(null);
			}
			userDO.setModifier(UserContext.getUsername());
			result = userService.updateById(userDO);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@RequestMapping("/delUserInfo")
	@ResponseBody
	public MessageResult<Integer> delUserInfo(HttpServletRequest request, HttpServletResponse response){
		MessageResult<Integer> result = null;
		try {
			String idStrs = request.getParameter("ids");
			result = userService.deleteById(Arrays.asList(idStrs.split(",")));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping("/checkUn")
	@ResponseBody
	public boolean checkUn(@RequestParam String username){
		boolean flag = false;
		try {
			UserDO userDO = new UserDO();
			userDO.setUsername(username);
			List<UserDO> userLst = userService.selectList(userDO);
			if(userLst.size()==0){
				flag = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	
}
